from . import models
from rest_framework import serializers


class CourseCategoryModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.CourseCategory
        fields = ['id', 'name']


class TeacherModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Teacher
        fields = ['id', 'name', 'role', 'image', 'brief', 'title', 'signature']


class CourseModelSerializer(serializers.ModelSerializer):
    # 只拿到关联的外键 teacher 表的 name 字段
    # teacher_name = serializers.CharField(max_length=32, source='teacher.name')

    # 序列化器嵌套（使用另外一个表的序列化器）
    # teacher = TeacherModelSerializer(many=True)
    # 注：加 'many=True' 即意味着一门课程对应多个老师，即课程是老师的外键
    # 注：序列化器嵌套时，另外一个序列化器对象的变量名要跟数据库中的外键字段相对应，或与外键别名'related_name' 相对应
    teacher = TeacherModelSerializer()

    # 不加 'many=True' 即意味着一个老师对应多门课程，即老师是课程的外键（符合咱们的表结构创建）
    class Meta:
        model = models.Course
        fields = ["id", "name", "course_img", "students", "lessons", "pub_lessons", "price", 'teacher', 'lesson_list',
                  'discount_name', 'real_price']


class CourseDedailModelSerializer(serializers.ModelSerializer):
    """
    课程详情页的序列化器
    """
    teacher = TeacherModelSerializer()

    class Meta:
        model = models.Course
        fields = ["id", "name", "course_img", "students", "lessons", "pub_lessons", "price", "teacher", "brief",
                  "level", 'level_name', 'brief_html', 'course_video', 'discount_name', 'real_price', 'active_time']


class CourseLessonModelSerializer(serializers.ModelSerializer):
    """
    课时序列化器
    """

    class Meta:
        model = models.CourseLesson
        fields = ['id', 'name', 'lesson', 'free_trail', 'duration']


class CourseChapterModelSerializer(serializers.ModelSerializer):
    """
    章节序列化器
    """
    coursesections = CourseLessonModelSerializer(many=True)

    class Meta:
        model = models.CourseChapter
        fields = ['id', 'name', 'chapter', 'coursesections']
